package com.mworld.galaxy.bull.entity.product;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.mworld.galaxy.bull.entity.StatefulIdEntity;

@Entity
@Table(name = "t_product")
@JsonAutoDetect(getterVisibility = Visibility.PUBLIC_ONLY)
public class Product extends StatefulIdEntity {
    private String title;
    private String desc;
    private long order;
    private double price;
    private Shop shop;
    private Set<Link> links = new HashSet<Link>(0);
    private Set<ProductImage> images = new HashSet<ProductImage>(0);

    public Product() {
        super();
    }

    public Product(Long id) {
        super(id);
    }

    @NotBlank
    @Column(name = "c_title")
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name = "c_desc")
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @NotNull
    @Column(name = "c_order")
    public long getOrder() {
        return order;
    }

    public void setOrder(long order) {
        this.order = order;
    }

    // JSR303 BeanValidator的校验规则
    @Min(0)
    @Max(100000000)
    @Column(name = "c_price")
    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @ManyToOne
    @JoinColumn(name = "c_shop_id")
    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "createDate ASC")
    public Set<Link> getLink() {
        return links;
    }

    public void setLink(Set<Link> category) {
        this.links = category;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "order ASC")
    public Set<ProductImage> getImages() {
        return images;
    }

    public void setImages(Set<ProductImage> images) {
        this.images = images;
    }

    @Transient
    public String getStatusString() {
        if (status == StatefulIdEntity.STATUS_ENABLED) {
            return "";
        } else {
            return "已下架";
        }
    }
}
